package com.lyt.service;

import com.alibaba.fastjson.JSON;
import com.lyt.bean.Question;
import com.lyt.dao.QuestionDao;
import com.lyt.util.BeanUtil;
import com.lyt.vo.AnswerVo;
import com.lyt.vo.ItemVo;
import com.lyt.vo.QuestionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * @Author: zjx
 * @Date: 2023/04/27/20:03
 * @Description:
 */
@Service
public class QuestionService {
    @Autowired
    QuestionDao questionDao;
    public QuestionVo getQuestionById(Integer id){
        System.out.println("传进来的id是" +id);
        Question question = questionDao.getQuestionById(id);
        System.out.println("查询到的question"+question);
        QuestionVo questionVo = new QuestionVo();

         BeanUtil.beanConvert(question,questionVo);//将question转换为questionVo
         List<ItemVo> items = (List<ItemVo>) JSON.parse(question.getContent());
         questionVo.setItems(items);
        if (questionVo.getType() == 2){
            List<AnswerVo> answerVos = (List<AnswerVo>) JSON.parse(question.getAnswer());//转为数组传送给前端
            questionVo.setAnswerVos(answerVos);
        }
        return questionVo;
    };

    //封装Question转为QuestionVo
    private List<QuestionVo> questionVos(List<Question> questions){
        List<QuestionVo> questionVos = new ArrayList<>();
        for (Question question : questions) {
            QuestionVo questionVo = new QuestionVo();
            BeanUtil.beanConvert(question,questionVo);
            List<ItemVo> items = (List<ItemVo>) JSON.parse(question.getContent());
            if (question.getType() == 2){//数据库的所有数据都要按最新标准，不然格式出错
                List<AnswerVo> answerVos = (List<AnswerVo>) JSON.parse(question.getAnswer());//转为数组传送给前端
                questionVo.setAnswerVos(answerVos);
            }
            questionVo.setItems(items);
            System.out.println(questionVo.toString());
            questionVos.add(questionVo);
            //将content转换
        }
        return questionVos;
    }

    //zjx 2023/5/7
   public List<QuestionVo> getAllQuestion(){
       List<Question> questions = questionDao.getAllQuestion();
       System.out.println(11111);
       return questionVos(questions);
   };

    public List<QuestionVo> getAllQuestionByCourseIdOrType(Integer type,Integer courseId){
        List<Question> questions = questionDao.getAllQuestionByCourseIdOrType(type,courseId);
        return questionVos(questions);
    }

    public int updateQuestion(QuestionVo questionVo){
        System.out.println("执行 updateQuestion");
        Question  question = new Question();
        Question  newQuestion = BeanUtil.beanConvert(questionVo,question);//将questionVo转换为question
        String contentJson = JSON.toJSONString(questionVo.getItems());

        //zjx
//        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(System.currentTimeMillis());
        System.out.println(formatter.format(date));
        newQuestion.setTime(formatter.format(date));

        System.out.println("选项的json串"+contentJson);
        newQuestion.setContent(contentJson);
        System.out.println("这是新创建的 对象"+ newQuestion);


        //zjx
        if (newQuestion.getType() == 2){
            List<AnswerVo> answerVos2 = new ArrayList<>();//用于重新接收答案数组，直接添加会添加修改前的数据
            for (int i = questionVo.getLength();i<questionVo.getAnswerVos().size();i++) {
                System.out.println(questionVo.getLength());
                System.out.println("这是答案数据长度"+questionVo.getAnswerVos().size());
                System.out.println(questionVo.getAnswerVos().get(i));
//                questionVo.getAnswerVos().get(i);
                answerVos2.add(questionVo.getAnswerVos().get(i));
            }

//            String answerJson = JSON.toJSONString(questionVo.getAnswerVos());
            String answerJson = JSON.toJSONString(answerVos2);
            newQuestion.setAnswer(answerJson);
        }

        //将选项转换为 json 串
        int num = questionDao.updateQuestion(newQuestion);
        if(num >0){
            System.out.println("修改试题成功");
            return 1;
        }
        return 1;
    };//根据试卷编号更新试卷

    public int deleteQuestion(Integer questionId){
        return questionDao.deleteQuestion(questionId);
    };//根据试卷编号删除试卷

    public int addQuestion(QuestionVo questionVo){
        System.out.println("执行 addQuestion");
        Question  question = new Question();
        Question  newQuestion = BeanUtil.beanConvert(questionVo,question);//将questionVo转换为question
        String contentJson = JSON.toJSONString(questionVo.getItems());

        //zjx
//        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(System.currentTimeMillis());
        System.out.println(formatter.format(date));
        newQuestion.setTime(formatter.format(date));

        System.out.println("选项的json串"+contentJson);
        newQuestion.setContent(contentJson);
        System.out.println("这是新创建的 对象"+ newQuestion);

        //zjx
        if (newQuestion.getType() == 2){
            String answerJson = JSON.toJSONString(questionVo.getAnswerVos());
            newQuestion.setAnswer(answerJson);
        }

        //将选项转换为 json 串
        int num = questionDao.addQuestion(newQuestion);
        if(num >0){
            System.out.println("新增试题成功");
            return 1;
        }
        return 0;
    };//创建试题

    //zjx 5/7
    public List<QuestionVo> getAllByCourseQuestion(int courseId){
        List<Question> questions = questionDao.getAllByCourseQuestion(courseId);
        return questionVos(questions);
    };//根据课程获取所有试题

    //zjx 5/7
    public List<QuestionVo> getAllBytypeuestion(int type){
        List<Question> questions = questionDao.getAllBytypeuestion(type);
        return questionVos(questions);
    };

    public List<Question> getAllByUserIdQuestion(int userId){
        return questionDao.getAllByUserIdQuestion(userId);
    };//根据创建者获取所有试题

    public List<Question> selectUnion(int paperId){
        return questionDao.selectUnion(paperId);
    };//获取所有试卷中的试题

    public List<Question> questionList(){
        return questionDao.getQuestionList();
    }
}
